package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishFLavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: SetMealServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 龙培
 * @Create 2025/3/3 13:48
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;
    /**
     * 保存套餐
     * @param setmealD
     */
    @Override
    public void save(SetmealDTO setmealD) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealD, setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealD.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        for (SetmealVO setmealVO : page) {
            List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetmealIds(setmealVO.getId());
            setmealVO.setSetmealDishes(setmealDishes);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除套餐
     */
    @Override
    public void delete(List<Long> ids) {
        //删除套餐表数据
        setmealMapper.delete(ids);
        //删除套餐菜品表数据
        setmealDishMapper.delete(ids);
    }

    @Override
    public SetmealVO getSetmealWithDishById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.SelectById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> dishBySetmealIds = setmealDishMapper.getDishBySetmealIds(id);
        setmealVO.setSetmealDishes(dishBySetmealIds);
        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐表数据
        setmealMapper.update(setmeal);
        //修改套餐菜品表数据
        setmealDishMapper.delete(List.of(setmealDTO.getId()));
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐的启售/停售
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        if (status == StatusConstant.ENABLE) {
            //如果套餐中的菜品停售的，不能启售套餐
            List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetmealIds(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);//直接使用update方法更新套餐状态
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
